import json

from flask import Blueprint, jsonify, request

from utils.db import db1
from utils.redisconn import r
from utils.comm import get_data,gettree

bp_workflow = Blueprint('workflow',__name__)


@bp_workflow.route('/worklist')
def worklist():
    sql = "select * from workflow"
    res = db1.find_all(sql)
    return jsonify({'code':200,'list':res})


@bp_workflow.route("/getsource")
def getsource():
    values = r.get_str("datasource")
    if values:
        lst = eval(values)
    else:
        sql = "select * from data_source"
        res = db1.find_all(sql)
        lst = []
        if res:
            for i in res:
                dic = {"id":i["id"],"name":i["name"]}
                msql = "select id,name,type from %s"%i["tablename"]
                mres = db1.find_all(msql)
                dic["mes"] = mres
                lst.append(dic)
        r.set_str("datasource",json.dumps(lst))

    return jsonify({"code":200,"list":lst})

@bp_workflow.route('/addworkflow',methods=["POST"])
def addworkflow():
    data = get_data()
    sql = "select * from workflow where id=%d"%int(data["id"])
    res = db1.find(sql)
    if res:
        usql = "update workflow set params='%s' where id=%d"%(str(data["list"]),int(res["id"]))
        db1.update(usql)
    return jsonify({"code":200,"msg":"修改成功"})

# 查询工作流
@bp_workflow.route("/query_workflow")
def query_workflow():
    sql = "select * from workflow"
    res = db1.find_all(sql)
    return jsonify({"code":200,"list":res})



@bp_workflow.route('/addtack', methods=['POST'])
def addtask():
    data = get_data()
    title = str(json.loads(data["params"])["姓名"]+data["title"])

    sql = "insert into task(title,params,status,userid,approver,next_approver) values('%s','%s',0,%d,'%s',%d)"%(title,data["params"],int(data["userid"]),data["approver"],int(json.loads(data["approver"])[0]))
    db1.update(sql)
    # key = "approver" + str(json.loads(data["approver"])[0])
    r.hset_key('approver',title,data["params"])
    return jsonify({"code":200,"msg":"添加成功"})

# 展示带用户的无限极分类
@bp_workflow.route('/getdept')
def getdept():
    sql = "select * from dept"

    res = db1.find_all(sql)

    list1 = gettree(res)

    return jsonify({'code':200,'list':list1})


# 查询请假工作流列表
#待我审批
@bp_workflow.route('/mytaskaudit')
def mytaskaudit():
    # id = request.args.get('id')
    # #从redis中获取我的任务列表
    # key='approver%d'%(int(id))
    # print(key)
    # task = r.hget_keys(key)
    task = r.hget_keys('approver')
    list=[]
    for k,v in task.items():
        id = str(k,encoding='utf8')
        title = str(v,encoding='utf8')
        list.append({'name':id,'title':title})
        # print(list)

    return jsonify({"code":200,'list':list})


# 查询请假工作流详情
@bp_workflow.route('/getoffid',methods=['GET'])
def getoffid():
    name = request.args.get('name')
    print(name)

    # task = r.hget_key('approver',name)
    # print(task)
    return "ok"
    # return jsonify({'code':200,'list':str(task,encoding='utf8')})


# @user_bp.route('/mytask')
# def mytask():
#     id = request.args.get("userid")
#     sql = "select * from users where id=%d"%int(id)
#     res = db1.find(sql)
#     print(res)
#     if res:
#         lst = []
#         key = "approver"+str(res["id"])
#         list = r.hget_keys(key)
#         for k,v in list.items():
#             lst.append({"id":str(k,encoding="utf8"),"title":str(v,encoding="utf8")})
#         return jsonify({"code":200,"list":lst})
#     return jsonify({"code":403,"msg":"id篡改"})



